<?php
// 商品
class OrderAction extends Action {
	
	private $cOrder = null;
	private $cTask  = null;
	function __construct(){
		//判断管理员登录状态
		//admin_login_check();
		
		import('@.ORG.Post');
		import('@.ORG.Task');
		
		import('@.ORG.Order');
		
    	$this->cOrder = new Order();
    	$this->cTask = new Task();
    	
	}
	
	//列表
    public function index(){

    	admin_role('Order:index');

    	$last_3days = '2013-06-01';
    	$tomorrow 	= short_date(time() + 86400);
    	
    	$begin 	= $this->_get('begin'	,'',$last_3days);
    	$end 	= $this->_get('end'		,'',$tomorrow);
    	
    	$search 	= $this->_get('search'		,'','');
    	$keywords 	= $this->_get('keywords'	,'','');
    	$page   	= $this->_get('p'			,'','1');
    	
    	//全部状态
    	$all_status = C('ALL_STATUS');
    	
    	//订单状态
    	$os   	= $this->_get('os'		,'',$all_status);
    	$ps  	= $this->_get('ps'		,'',$all_status);
    	$ss   	= $this->_get('ss'		,'',$all_status);
    	    	
    	$pageSize 	= 25;
    	
    	
    	
    	import('@.ORG.Order');
    	$cOrder = new Order();
    	
    	$where = array(
    		'begin' 	=> $begin,
    		'end' 		=> $end,
    		'search' 	=> $search,
    		'keywords' 	=> $keywords,
    		'order_status'		=>$os,
    		'pay_status'		=>$ps,
    		'shipping_status'	=>$ss
    	);
    	    	
    	$allCount = 0;
    	//订单列表
    	$orders = $cOrder->lists($where,$page,$pageSize,$allCount);
    	$orders = $cOrder->orders_append($orders);
    	
    	//查询条件 
    	$order_search = C('ORDER_SEARCH');
        
    	//订单，支付，发货状态
    	$order_status 	= C('ORDER_STATUS');
    	$pay_status		= C('PAY_STATUS');
    	$shipping_status= C('SHIPPING_STATUS');
    	
    	/**
		 +------------------------------------------------------------------------------
		 * 分页
		 +------------------------------------------------------------------------------
		 */
		//导入分页类
		import("@.ORG.Page");
		
		$pageParams = array(
			'g'		=> 'Admin',
			'begin' => $begin,
			'end'	=> $end,
			'search' => $search,
			'keywords' => $keywords,
   			'os'			=> $os,
    		'ps'			=> $ps,
    		'ss'			=> $ss
		);
		 // 实例化分页类
	  	$p = new Page($allCount,$pageSize,$pageParams);
	  	
    	// 分页显示输出
    	$pager = $p->show();

		/**
		 +------------------------------------------------------------------------------
		 * 分页
		 +------------------------------------------------------------------------------
		 */
    	
    	$this->assign(
    		array(
    			'APP_NAME'	=> APP_NAME,
    			'orders'	=> $orders,
    			'order_search'			=> $order_search,
    			'begin'		=> $begin,
    			'end'		=> $end,
    			'search'	=> $search,
    			'keywords'	=> $keywords,
    			'pager'		=> $pager,
				'order_status' 		=> $order_status,
    			'pay_status' 		=> $pay_status,
    			'shipping_status' 	=> $shipping_status,
    			'os'			=> $os,
    			'ps'			=> $ps,
    			'ss'			=> $ss,
    			'all_status'	=> $all_status,
    			'MainPage'	=> 'Order:index'
    		)
    	);
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    //详情或修改
    public  function detail(){
    	
    	admin_role('Order:detail');
    	
    	$id 	= $this->_get('id'	,'',0);
				
    	import('@.ORG.Order');
    	$cOrder = new Order();
    	
    	//订单详情
    	$order = $cOrder->get_by_id($id);
    	//附加订单状态
    	$order = $cOrder -> order_append($order);

    	import('@.ORG.OrderGoods');
    	$cOrderGoods = new OrderGoods();
    	
    	//商品列表
    	$orderGoods = $cOrderGoods->get_by_order_id($id);
    	
    	//支付记录
    	import('@.ORG.PayLog');
    	$cPayLog = new PayLog();
    	
    	$payLogs = $cPayLog->get_by_order_id($id);
    	
    	
    	//订单操作记录
    	import('@.ORG.OrderLog');
    	$cOrderLog = new OrderLog();
    		
    	$orderLogs = $cOrderLog->get_by_order_id($id);
    	$orderLogs = $cOrder -> orders_append($orderLogs);
    	
    	//订单，支付，发货状态
    	$order_status 	= C('ORDER_STATUS');
    	$pay_status		= C('PAY_STATUS');
    	$shipping_status= C('SHIPPING_STATUS');
    
    	$this->assign(
    		array(
    			'APP_NAME'	=> APP_NAME,
    			'order'		=> $order,
    			'order_goods' 		=> $orderGoods,
    			'order_status' 		=> $order_status,
    			'pay_status' 		=> $pay_status,
    			'shipping_status' 	=> $shipping_status,
    			'order_logs'		=> $orderLogs,
    			'pay_logs'			=> $payLogs,
    			'MainPage'			=> 'Order:detail'
    		)
    	);
    	
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    
    //详情或修改
    public  function consignee(){
    	
    	admin_role('Order:detail');
    	
    	$id 	= $this->_get('id'	,'',0);
				
    	import('@.ORG.Order');
    	$cOrder = new Order();
    	
    	//订单详情
    	$order = $cOrder->get_by_id($id);
    	//附加订单状态
    	$order = $cOrder -> order_append($order);

    	
    	$this->assign(
    		array(
    			'APP_NAME'	=> APP_NAME,
    			'order'		=> $order,    		
    			'MainPage'	=> 'Order:consignee'
    		)
    	);
    	
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    
 	//详情或修改
    public  function cost_update(){
    	
    	admin_role('Order:detail');
    	
    	$id 	= $this->_get('id'	,'',0);
				
    	import('@.ORG.Order');
    	$cOrder = new Order();
    	
    	//订单详情
    	$order = $cOrder->get_by_id($id);

    	$this->assign(
    		array(
    			'APP_NAME'	=> APP_NAME,
    			'order'		=> $order,    		
    			'MainPage'	=> 'Order:cost_update'
    		)
    	);
    	
    	$this->display(C('ADM_TEMPLATE'));
    }

    
	function cost_update_do(){
    	
    	admin_role('Order:detail');
     	
    	$order_id 		= $this->_post('order_id'		,'',0);
    	$shipping_fee 	= $this->_post('shipping_fee'	,'',0);
    	$pack_fee 		= $this->_post('pack_fee'		,'',0);
    	$discount 		= $this->_post('discount'		,'',0);
    	$paid_amount 	= $this->_post('paid_amount'	,'',0);

    	import('@.ORG.Order');
    	$cOrder = new Order();

    	$upItems = array(
	    	'shipping_fee' 		=> $shipping_fee,
	    	'pack_fee' 			=> $pack_fee,
	    	'discount' 			=> $discount,
	    	'paid_amount' 		=> $paid_amount
    	);
    	
    	$up_result = $cOrder -> update($order_id,$upItems);
    	
    	json_out($up_result);
    }
    
    
    //订单操作记录
    public function order_log_add(){
    	
    	admin_role('Order:detail');
    	
    	$order_id 	= $this->_post('order_id'	,'',0);
    	$message 	= $this->_post('message'	,'','');
    	
    	//订单状态
    	$order_status 		= $this->_post('order_status'		,'','0');
    	$pay_status 		= $this->_post('pay_status'			,'','0');
    	$shipping_status 	= $this->_post('shipping_status'	,'','0');
    	
    	//修改后的订单状态
    	$upItems = array(
    		'order_status' 		=> $order_status,
    		'pay_status' 		=> $pay_status,
    		'shipping_status' 	=> $shipping_status
    	);
    	
    	import('@.ORG.Order');
    	$cOrder = new Order();
    	
    	$cOrder->update($order_id,$upItems);    	
    	
    	//添加订单操作记录
    	import('@.ORG.OrderLog');
    	$cOrderLog = new OrderLog();
    	
    	$add_result = $cOrderLog->add($order_id,$message);
    	
    	json_out($add_result);
    }
    
     public function order_log(){
     	
     	admin_role('Order:detail');
     	
     	$order_id 	= $this->_get('order_id'	,'',0);
     	
     	//订单操作记录
    	import('@.ORG.OrderLog');
    	$cOrderLog = new OrderLog();
    	
    	$orderLogs = $cOrderLog -> get_by_order_id($order_id);
    	
    	import('@.ORG.Order');
    	$cOrder = new Order();
    	
    	$orderLogs = $cOrder -> orders_append($orderLogs);
    	
     	$this->assign(
    		array(
    			'order_logs'		=> $orderLogs
    		)
    	);
    	
     	$this->display('Fragment:order_log');
     }
    
    //为订单添加商品
    function order_goods_add(){
    	
    	admin_role('Order:goods_update');
    	
    	$order_id = $this->_post('order_id'	,'',0);
    	$goods_id = $this->_post('goods_id'	,'',0);
    	$goods_attr = $this->_post('goods_attr'	,'',0);
				

    	import('@.ORG.OrderGoods');
    	$cOrderGoods = new OrderGoods();

    	//订单中的商品
    	$order_goods = $cOrderGoods -> get_by_goods_id($order_id,$goods_id,$goods_attr);
    	$goods_count = 0;
    	if(!is_null($order_goods)){
    		//商品的数量
    		$goods_count = $order_goods['goods_number'];
    		
    		json_out(false, '要添加的商品已经在购物车中共'.$goods_count.'个');
    	}else{
    		
    		//添加商品到订单
    		import('@.ORG.Goods');
    		$cGoods = new Goods();

    		//默认值
    		$goods_number 	= 1;
    		$goods_price	= $cGoods->get_price($goods_id,$goods_attr);
    		
    		
    		$add_result = $cOrderGoods ->add($order_id,$goods_id,$goods_number,$goods_price,$goods_attr);
    		
    		if($add_result){
    			
    			//更新商品总金额 
    			import('@.ORG.Order');
    			$cOrder = new Order();
    			
    			$cOrder->update_goods_amount($order_id);
    			
    			json_out(true, '商品添加到订单成功');
    		}else{
    			json_out(false, '商品添加到订单失败');
    		}
    	}
    	
    	json_out($goods_count);
    }
    
    
     //删除订单商品
    function order_goods_del(){
    	
    	admin_role('Order:goods_update');
    	
    	$id = $this->_post('id'	,'',0);
				
    	
    	import('@.ORG.OrderGoods');
    	$cOrderGoods = new OrderGoods();

    	//商品信息
    	$goods = $cOrderGoods -> get_by_id($id);
  
    	$del_result = $cOrderGoods -> del_by_id($id);
    	
    	if($del_result){
   	  		//更新商品总金额 
  			import('@.ORG.Order');
   			$cOrder = new Order();
    			
   			$cOrder->update_goods_amount($goods['order_id']);
    	}

    	json_out($del_result);
    }
     
   	//更新订单商品数量
    function consignee_save(){
    	
    	admin_role('Order:detail');
    	
    	$order_id 		= $this->_post('order_id'		,'',0);
    	$consignee 		= $this->_post('consignee'		,'','');
    	$mobile_phone 	= $this->_post('mobile_phone'	,'','');
    	$address 		= $this->_post('address'		,'','');
    	$pay_id 		= $this->_post('pay_id'			,'','1');
    	$delivery_time 	= $this->_post('delivery_time'	,'','');
    	$postscript 	= $this->_post('postscript'	,'','');

    	import('@.ORG.Order');
    	$cOrder = new Order();

    	$upItems = array(
	    	'consignee' 		=> $consignee,
	    	'mobile_phone' 		=> $mobile_phone,
	    	'address' 			=> $address,
    		'pay_id'			=> $pay_id,
	    	'delivery_time' 	=> $delivery_time,
    		'mobile_phone'		=> $mobile_phone,
    		'postscript' 		=> $postscript
    	);
    	
    	$up_result = $cOrder -> update($order_id,$upItems);
    	
    	json_out($up_result);
    }
    
    //更新订单商品数量
    function order_goods_update(){
    	
    	admin_role('Order:goods_update');
    	
    	$id 			= $this->_post('id'				,'',0);
    	$goods_number 	= $this->_post('goods_number'	,'',0);
				
    	import('@.ORG.OrderGoods');
    	$cOrderGoods = new OrderGoods();

    	$upItems = array(
    		'goods_number' => $goods_number
    	);
    	
    	$up_result = $cOrderGoods -> update($id,$upItems);
    	
    	if($up_result){
    		
    		//商品信息
    		$goods = $cOrderGoods -> get_by_id($id);
    	
			//更新商品总金额 
  			import('@.ORG.Order');
   			$cOrder = new Order();
    			
   			$cOrder->update_goods_amount($goods['order_id']);
    	}
    	
    	json_out($up_result);
    }
    
    
    //订单商品修改
    public  function goods_update(){
    	
    	admin_role('Order:goods_update');
    	
    	$order_id = $this->_get('order_id'	,'',0);
				
    	import('@.ORG.Order');
    	$cOrder = new Order();
    	
    	//订单详情
    	$order = $cOrder->get_by_id($order_id);
    	//附加订单状态
    	$order = $cOrder -> order_append($order);

    	import('@.ORG.OrderGoods');
    	$cOrderGoods = new OrderGoods();
    	
    	//商品列表
    	$orderGoods = $cOrderGoods->get_by_order_id($order_id);
    	
    	//商品各类数量
    	$goods_count = count($orderGoods);
    	
    	//商品总数
    	$sum_goods_number 	= 0;
    	//商品总金额
    	$sum_goods_price	= 0;
    	foreach ($orderGoods as $g){
    		$sum_goods_number 	+= $g['goods_number'];
    		$sum_goods_price	+= $g['subtotal'];
    	}
    	
    	
    	//商品列表用于添加商品
    	import('@.ORG.Goods');
    	$cGoods = new Goods();
    	
    	$goods  = $cGoods->lists($goods_id = null,$page=1,$pageSize=999999,$allCount);
    	
    	$this->assign(
    		array(
    			'order'		=> $order,
    			'order_goods' 		=> $orderGoods,
    			
    			'goods_count' 		=> $goods_count,
    			'sum_goods_number' 	=> $sum_goods_number,
    			'sum_goods_price' 	=> money($sum_goods_price),
    			'goods'				=> $goods,
    			'MainPage'			=> 'Order:goods_update'
    		)
    	);
    	
    	$this->display(C('ADM_TEMPLATE'));
    }
    
 	//订单商品修改
    public  function goods_update_goods(){
    	
    	admin_role('Order:goods_update');
    	
    	$order_id = $this->_get('order_id'	,'',0);
				
    	import('@.ORG.Order');
    	$cOrder = new Order();
    	
    	import('@.ORG.OrderGoods');
    	$cOrderGoods = new OrderGoods();
    	
    	//商品列表
    	$orderGoods = $cOrderGoods->get_by_order_id($order_id);
    	
    	//商品各类数量
    	$goods_count = count($orderGoods);
    	
    	//商品总数
    	$sum_goods_number 	= 0;
    	//商品总金额
    	$sum_goods_price	= 0;
    	foreach ($orderGoods as $g){
    		$sum_goods_number 	+= $g['goods_number'];
    		$sum_goods_price	+= $g['subtotal'];
    	}
    	
    	$this->assign(
    		array(
    			'order_goods' 		=> $orderGoods,
    			'goods_count' 		=> $goods_count,
    			'sum_goods_number' 	=> $sum_goods_number,
    			'sum_goods_price' 	=> money($sum_goods_price),
    			'MainPage'			=> 'Fragment:goods_update'
    		)
    	);
    	
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    function goods_attr(){
    	
    	$goods_id = $this->_get('goods_id'	,'',0);
    	
    	import('@.ORG.GoodsAttr');
    	$cGoodsAttr = new GoodsAttr();
    	
    	//属性价格
    	$attrs = $cGoodsAttr->get_by_goods_id($goods_id);
    	
    	$this->assign(
    		array(
    			'attrs' => $attrs
       		)
    	);
    	
    	$this->display('Fragment:goods_attrs');
    }
    

    
    /**
     * 
     * 修改订单
     */
    function order_update(){
    	
    	admin_role('Task:index');
    	
	    $post = post(array(
	    	array('task_id','intval'	,0),
    		array('order_id','intval'	,0),
	    	array('item'	,'trim'		,''),
    		array('value'	,'trim'		,'')
    	));

    	$task_id 	= $post['task_id'];
		$order_id 	= $post['order_id'];
		$item 		= $post['item'];
		$value 		= $post['value'];

		//允许修改的
		$allow_item = array(
			'order_sn',
			'shipping_id',
			'invoice_no',
			'goods_amount',
			'bonus_money',
			'shipping_fee',
			'paid_amount',
			'remark',
			'is_exception',
            'receive_status',
            'is_finish'
		);
		
		if(!in_array($item, $allow_item)){
			json_error('参数错误,不在允许修改范围内!');
		}
		
    	$upItems = array(
	    	$item 		=> $value
    	);

    	//修改的是快递号,
    	if($item == 'invoice_no' && $value != ''){
    		$exist_order = $this->cOrder->invoice_no_exist($value,$task_id);
    		
    		if($exist_order){
    			json_error('快递号已经在该平台下使用,任务单号:'.$exist_order['task_id'].',订单编号:'.$exist_order['order_id']);
    		}
    	}

        //下单ip
        $order_ip = '';
        //如果修改的订单号，同时记录下单ip
        if($item == 'order_sn'){
            //获取订单
            $order = $this->cOrder->get_by_id($order_id);

            //原下单ip
            $old_order_id = $order['order_ip'];
            //原下单ip为空时修改下单ip
            if(empty($old_order_id)){
                $order_ip = real_ip();
                $upItems['order_ip'] = $order_ip;
            }

            $upItems['ordered_time'] = full_date();
        }

    	//更新内容
    	$up_result = $this->cOrder ->update($order_id,$upItems);

    	if($up_result){
    		
    		//填写的是订单号
    		if($item == 'order_sn'){
    			
    			$where 		= array();
    			$page 		= 1;
    			$pageSize 	= PHP_INT_MAX;
    			$allCount	= 0;
    			
    			//订单数量
    			$orders = $this->cOrder->task_orders($task_id,$where,$page,$pageSize,$allCount);
    	
    			$order_count = count($orders);
    		    //已完成的数量
	    		$finish_count = $this->cTask->task_order_finish_count($task_id);
	    		
	    		//第一个开始，变成执行中
	    		if($finish_count == 1){
	    			
	    			//设置成执行中
	    			$msg = '';
	    			$this->cTask->update_task_status($task_id,TS_RUNNING,$msg);
	    		
	    		}
    		}
    		
    		json_success('订单修改成功',array('order_ip'=>$order_ip));
    	}else{
    		json_error('订单修改失败');
    	}
    }

    /**
     *
     * 修改订单已完成
     */
    function order_set_finish(){

        admin_role('Task:index');

        $post = post(array(
            array('order_id','intval'	,0),
            array('order_sn','trim'		,''),


            array('bonus_money','floatval'		,'0'),
            array('paid_amount','floatval'		,'0'),
            array('shipping_fee','floatval'		,'0'),

        ));

        $order_id 	= $post['order_id'];
        $order_sn   = $post['order_sn'];


        $bonus_money   = $post['bonus_money'];
        $paid_amount   = $post['paid_amount'];
        $shipping_fee   = $post['shipping_fee'];


        //获取订单
        $order = $this->cOrder->get_by_id($order_id);

        //已完成
        $is_finish = $order['is_finish'];

        if($is_finish){
            json_success('订单已完成，无需重复操作');
        }

        //原下单ip
        $old_order_sn = $order['order_sn'];
        $old_order_ip = $order['order_ip'];
        $old_ordered_time = $order['ordered_time'];

        $upItems['is_finish'] = 1;

        //原下单ip为空时修改下单ip
        if($old_order_sn == ''){
            $upItems['order_sn'] = $order_sn;
        }
        if(empty($old_order_ip)){
            $upItems['order_ip'] = real_ip();
        }else{
            $upItems['order_ip'] = $old_order_ip;
        }

        if(empty($old_ordered_time)){
            $upItems['ordered_time'] = full_date();
        }

        $upItems['bonus_money'] = $bonus_money;
        $upItems['paid_amount'] = $paid_amount;
        $upItems['shipping_fee'] = $shipping_fee;

        //更新内容
        $up_result = $this->cOrder ->update($order_id,$upItems);

        $task_id = $order['task_id'];
        //已完成的数量
        $finish_count = $this->cTask->task_order_finish_count($task_id);

        //第一个开始，变成执行中
        if($finish_count == 1){

            //设置成执行中
            $msg = '';
            $this->cTask->update_task_status($task_id,TS_RUNNING,$msg);

        }


        if($up_result){
            json_success('订单完成操作成功',array('order_ip'=>$upItems['order_ip']));
        }else{
            json_error('订单完成操作失败');
        }
    }
}
?>